<template>
    <view class="motif-container">
        <addHeader :title="'主题'"></addHeader>
        <view class="motif-content">
            <view class="form-content">
                <uni-forms ref="customForm" :modelValue="formData" label-width="85px">
                    <uni-forms-item label="圈子名称" name="title">
                        <uni-data-select v-model="formData.circleId" :localdata="circleData" placeholder="请选择圈子名称"></uni-data-select>
                    </uni-forms-item>
                    <uni-forms-item label="主题名称" name="title">
                        <uni-easyinput v-model="formData.name" placeholder="请输入主题名称" />
                    </uni-forms-item>
                    <uni-forms-item label="主题内容" name="content">
                        <uni-easyinput v-model="formData.content" type="textarea" placeholder="请输入主题内容" />
                    </uni-forms-item>
                    <uni-forms-item label="" name="accessory">
                        <uni-file-picker fileMediatype="image" @select="onUpload"  limit="1"></uni-file-picker>
                    </uni-forms-item>
                    <uni-forms-item><u-button @click="onSubmitClick" type="primary">立即添加</u-button></uni-forms-item>
                </uni-forms>
            </view>
        </view>
    </view>
</template>

<script>
    import addHeader from './add-header.vue';
    import { getCircleData } from '@/api/work'
    import config from '@/config'
    const baseUrl = config.baseUrl
    export default {
        data() {
            return {
                circleData:[],
                formData:{
                    name:"",
                    circleId:"",
                    content:"",
                    accessory:"",
                    deviceId: this.$store.state.user.deviceId,
                }
            }
        },
        components:{addHeader},
        created(){
            this.getCircleDataInfo();
        },
        methods: {
            //获取圈子课表信息
            getCircleDataInfo(){
                getCircleData({deviceId: this.$store.state.user.deviceId}).then(res=>{
                    res.circles.forEach(item=>{
                        this.circleData.push({value:item.id,text:item.title})
                    })
                })
            },
            onUpload(e){
                this.formData.accessory=e.tempFilePaths[0];
            },
            onSubmitClick(){
                uni.showLoading({title: '正在保存'})
                uni.uploadFile({
                    url: baseUrl+'/circle/saveTheme',
                    filePath: this.formData.accessory,
                    name: 'file',
                    formData: this.formData,
                    success: (res) => {
                        if(res.statusCode == 200){
                            uni.$u.toast('保存成功');
                            setTimeout(() => {
                                this.$tab.reLaunch('/pages/work/index')
                            }, 2000);
                        }
                    }
                });
            }
        }
    }
</script>

<style lang="scss">
    .motif-content{
        background: #fff;
        padding: 15px;
    }
</style>
